home *** CD-ROM | disk | FTP | other *** search
Wrap
Text File | 1992-12-24 | 6.9 KB | 217 lines | [ TEXT/PJMM]
unit LexicalAnalysis; interface uses Globals; procedure lexicalanalysis (var line: str255; var removeblanks: boolean; var ntot: longint; var sy, tokentype: hdlstringarray0; var pr: hdlintarray0; var error: str255); implementation procedure lexicalanalysis; label 99, 999, 9999; var i, j, k, place, len, numstrings, mtot: longint; s1, s2, s3, s4, s5: boolean; ind, nst, nend: hdlintarray0; astr: hdlstringarray0; ch, ch1, ch2, ch3: char; ty: hdlstringarray0; x: extended; flag: hdlflagtype; begin ind := hdlintarray0(NewHandle(SizeOf(intarray0))); nst := hdlintarray0(NewHandle(SizeOf(intarray0))); nend := hdlintarray0(NewHandle(SizeOf(intarray0))); astr := hdlstringarray0(NewHandle(SizeOf(stringarray0))); place := pos(semicolon, line); if place = 0 then {Putting a semicolon at the end of the statement,line, for a} line := concat(line, ';'); {delimiter, if it isn't already there.} removeblanks := false; if removeblanks then begin place := pos(blank, line); while place <> 0 do begin delete(line, place, 1); place := pos(blank, line); end; end; for i := 1 to length(line) do begin ind^^[i] := 0; {Initialize ind^^[i] array.} k := ord(line[i]); if ((65 <= k) and (k <= 90)) or ((97 <= k) and (k <= 122)) then ind^^[i] := 1; {if line[i] is a letter of alphabet, set ind^^[i] = 1} if ((48 <= k) and (k <= 57)) or (k = 46) then ind^^[i] := 2; {if line[i] is a number or decimal, set ind^^[i] = 2} end; numstrings := 0; for i := 1 to length(line) do begin if (i = 1) and ((ind^^[i] = 1) or (ind^^[i] = 2)) then begin numstrings := numstrings + 1; {if first character is 1 or 2, string starts} nst^^[numstrings] := i; {at the first character position of line} end; if i > 3 then if (ind^^[i] = 2) and ((line[i - 1] = '+') or (line[i - 1] = '-')) then if ((line[i - 2] = 'e') or (line[i - 2] = 'E')) and ((ind^^[i - 3] = 2)) then goto 999; if i > 4 then if (ind^^[i] = 2) and (ind^^[i - 1] = 2) and ((line[i - 2] = '+') or (line[i - 2] = '-')) then if ((line[i - 3] = 'e') or (line[i - 3] = 'E')) and ((ind^^[i - 4] = 2)) then goto 999; if i > 1 then if (ind^^[i] <> 0) and (ind^^[i - 1] = 0) then begin numstrings := numstrings + 1; {Start of string at ith position if 1 or 2 follows} nst^^[numstrings] := i; {a 0 after the first character position.} end; if i > 1 then if (ind^^[i] = 0) and (ind^^[i - 1] <> 0) then {End of string at (i-1)th position if} nend^^[numstrings] := i - 1; {ith is a 0 and (i-1)the is <> 0. } if (i = length(line)) then if ((ind^^[i] = 1) or (ind^^[i] = 2)) then nend^^[numstrings] := i; 999: end; for i := 1 to numstrings do astr^^[i] := nil; for i := 1 to numstrings do if astr^^[i] = nil then begin astr^^[i] := hdlstringsize(NewHandle(SizeOf(stringsize))); astr^^[i]^^ := ''; {Initialize astr^^[i]^^ strings to null strings.} end; for i := 1 to numstrings do astr^^[i]^^ := copy(line, nst^^[i], nend^^[i] + 1 - nst^^[i]); DisposHandle(handle(ind)); {Free up space on the heap.} ntot := 0; for i := 1 to numstrings do {Meshing strings and operators to get} for j := 1 to length(line) do {tokens, sy^^[i]^^, i = 1, ntot} begin s1 := (j < nst^^[i]) and (i = 1); s2 := (nend^^[i] < j) and (j < nst^^[i + 1]) and (i < numstrings); s3 := (nend^^[i] < j) and (i = numstrings); if s1 or s2 or s3 then begin ntot := ntot + 1; sy^^[ntot] := hdlstringsize(NewHandle(SizeOf(stringsize))); sy^^[ntot]^^ := line[j]; goto 9999; end; if (nst^^[i] = j) then begin ntot := ntot + 1; sy^^[ntot] := hdlstringsize(NewHandle(SizeOf(stringsize))); sy^^[ntot]^^ := astr^^[i]^^; goto 9999; end; if (nst^^[i] < j) and (j <= nend^^[i]) then goto 9999; 9999: end; sy^^[0] := hdlstringsize(NewHandle(SizeOf(stringsize))); sy^^[0]^^ := '@'; DisposHandle(handle(nst)); DisposHandle(handle(nend)); {Free up space on the heap.} DisposHandle(handle(astr)); ty := hdlstringarray0(NewHandle(SizeOf(stringarray0))); mtot := -1; for i := 0 to ntot do if sy^^[i]^^ <> blank then begin mtot := mtot + 1; ty^^[mtot] := hdlstringsize(NewHandle(SizeOf(stringsize))); ty^^[mtot]^^ := sy^^[i]^^; end; ntot := mtot; for i := 1 to ntot do sy^^[i]^^ := ty^^[i]^^; DisposHandle(handle(ty)); for i := 0 to ntot do {setting token types, tokentype^^[i]^^, i = 1, ntot} begin tokentype^^[i] := hdlstringsize(NewHandle(SizeOf(stringsize))); tokentype^^[i]^^ := 'string'; if (sy^^[i]^^ = exponent) or (sy^^[i]^^ = asterisk) or (sy^^[i]^^ = crosshatch) or (sy^^[i]^^ = leftslash) or (sy^^[i]^^ = rightslash) or (sy^^[i]^^ = plus) or (sy^^[i]^^ = minus) or (sy^^[i]^^ = equals) or (sy^^[i]^^ = rightparen) or (sy^^[i]^^ = semicolon) or (sy^^[i]^^ = leftparen) or (sy^^[i]^^ = ampersand) then tokentype^^[i]^^ := 'binary'; if (sy^^[i]^^ = 'pi') or (tokentype^^[i]^^ = 'string') and (((48 <= ord(sy^^[i]^^[1])) and (ord(sy^^[i]^^[1]) <= 57)) or (ord(sy^^[i]^^[1]) = 46)) then tokentype^^[i]^^ := 'constant'; if (sy^^[i]^^ = '''') or (sy^^[i]^^ = 'abs') or (sy^^[i]^^ = 'inv') or (sy^^[i]^^ = 'sqrt') or (sy^^[i]^^ = 'sin') or (sy^^[i]^^ = 'exp') or (sy^^[i]^^ = 'ln') then tokentype^^[i]^^ := 'function'; if (tokentype^^[i]^^ = 'string') and (tokentype^^[i]^^ <> 'binary') and (tokentype^^[i]^^ <> 'constant') and (tokentype^^[i]^^ <> 'function') then tokentype^^[i]^^ := 'variable'; if i > 0 then begin s1 := ((sy^^[i]^^ = plus) or (sy^^[i]^^ = minus)); s2 := (tokentype^^[i - 1]^^ <> 'variable') and (tokentype^^[i - 1]^^ <> 'constant'); s3 := (sy^^[i - 1]^^ <> rightparen) and (sy^^[i - 1]^^ <> quote); if (s1 and s2 and s3) then tokentype^^[i]^^ := 'unary'; end; end; for i := 0 to ntot do begin if (sy^^[i]^^ = exponent) then pr^^[i] := 8; if (tokentype^^[i]^^ = 'function') then pr^^[i] := 7; if (sy^^[i]^^ = asterisk) or (sy^^[i]^^ = crosshatch) or (sy^^[i]^^ = rightslash) or (sy^^[i]^^ = leftslash) then pr^^[i] := 6; if (sy^^[i]^^ = plus) or (sy^^[i]^^ = minus) then pr^^[i] := 5; if sy^^[i]^^ = equals then pr^^[i] := 4; if (sy^^[i]^^ = rightparen) or (sy^^[i]^^ = semicolon) then pr^^[i] := 3; if sy^^[i]^^ = leftparen then pr^^[i] := 2; if sy^^[i]^^ = '@' then pr^^[i] := 1; if (tokentype^^[i]^^ <> 'function') and (tokentype^^[i]^^ <> 'binary') then pr^^[i] := 0; end; 99: end; end.